package com.chengliang.bbs.type.converter;

import com.chengliang.bbs.type.base.BaseEnum;
import org.springframework.core.convert.converter.Converter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: chengliang
 * @create: 2019-07-24 21:35
 * 枚举编码 -> 枚举 转化器
 */
@Deprecated
public class CodeToEnumConverter<T extends BaseEnum> implements Converter<Integer, T> {

    private Map<Integer, T> enumMap = new HashMap<>();

    public CodeToEnumConverter(Class<T> enumType) {
        System.out.println("初始化枚举列表");
        T[] enums = enumType.getEnumConstants();
        for (T e : enums) {
            enumMap.put(e.getCode(), e);
        }
    }

    @Override
    public T convert(Integer source) {
        System.out.println("获取枚举类型");
        T t = enumMap.get(source);
        if (t == null || t.equals("")) {
            throw new IllegalArgumentException("无法匹配对应的枚举类型");
        }
        return t;
    }
}
